﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace LINQPad.OpenAccess.UI.View
{
    /// <summary>
    /// Used to expose a bindable password property for the PasswordBox.
    /// Also that approach isn't really recommended due to security reasons, its sufficient for this case.
    /// </summary>
    public class PasswordBoxHacks : DependencyObject
    {
        /// <summary>
        /// Gets a value indicating whether the password box hacks are enabled.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static bool GetEnabled( DependencyObject obj )
        {
            return ( bool ) obj.GetValue( EnabledProperty );
        }
        /// <summary>
        /// Gets the password.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns></returns>
        public static string GetPassword( DependencyObject obj )
        {
            return ( string ) obj.GetValue( PasswordProperty );
        }
        /// <summary>
        /// Sets the enabled state of the password box hacks.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="value">set to <c>true</c> to enable.</param>
        public static void SetEnabled( DependencyObject obj, bool value )
        {
            obj.SetValue( EnabledProperty, value );
        }
        /// <summary>
        /// Sets the password.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="value">The value.</param>
        public static void SetPassword( DependencyObject obj, string value )
        {
            obj.SetValue( PasswordProperty, value );
        }

        /// <summary>
        /// The password dependency property
        /// </summary>
        public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached( "Password", typeof( string ), typeof( PasswordBoxHacks ), new PropertyMetadata( null, Password_PropertyChanged ) );
        /// <summary>
        /// The enabled dependency property.
        /// </summary>
        public static readonly DependencyProperty EnabledProperty = DependencyProperty.RegisterAttached( "Enabled", typeof( bool ), typeof( PasswordBoxHacks ), new PropertyMetadata( false, Enabled_PropertyChanged ) );


        private static void Enabled_PropertyChanged( DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e )
        {
            var passwordBox = dependencyObject as PasswordBox;
            if ( passwordBox != null )
            {
                passwordBox.PasswordChanged -= PasswordBox_PasswordChanged;

                if ( ( bool ) e.NewValue )
                {
                    passwordBox.Password = GetPassword( passwordBox );
                    passwordBox.PasswordChanged += PasswordBox_PasswordChanged;
                }
            }
        }
        private static void Password_PropertyChanged( DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e )
        {
            var passwordBox = dependencyObject as PasswordBox;
            if ( passwordBox != null && GetEnabled( passwordBox ) )
            {
                passwordBox.PasswordChanged -= PasswordBox_PasswordChanged;
                passwordBox.Password = e.NewValue as string;
                passwordBox.PasswordChanged += PasswordBox_PasswordChanged;
            }
        }
        private static void PasswordBox_PasswordChanged( object sender, RoutedEventArgs e )
        {
            var passwordBox = sender as PasswordBox;
            if ( passwordBox != null )
            {
                SetPassword( passwordBox, passwordBox.Password );
            }
        }
    }
}
